<?php 
	//负责每日自动结算的自动程序
	//中文编码
	header("Content-type:text/html;charset=utf-8");
	include_once("base.php") ;
	class auto extends  base{
		//准备数据   配置项（比例，增加字段） 数据库  
		function __construct(){
			set_time_limit(0);
			parent::__construct();
			$configsql="select * from yang_config";
			$list=$this->select($configsql);
			foreach ($list as $k=>$v){
			    $list[$v['key']]=$v['value'];
			}
			$this->config = $list;
		}
		//开始自动程序
		public function run(){
		    error_reporting( E_ALL&~E_NOTICE );
		    //设置时区
		    date_default_timezone_set('PRC');
		    //抓数据 添加数据库
		    $this->getCoinPrice();
		    $this->runFenhongMoneyDay(time());
			echo "结算完成";
			die;
		}
		//抓价格 存数据库
		public function getCoinPrice(){
		    $price1 = $this->config['vic_price'];
		    //比特币
		    $url="https://api.huobi.pro/market/detail/merged?symbol=btcusdt";
		    $result=$this->curl_get($url);
		    $price2=$result['tick']['close'];
		    //以太坊
		    $url1="https://api.huobi.pro/market/detail/merged?symbol=ethusdt";
		    $result1=$this->curl_get($url1);
		    $price3=$result1['tick']['close'];
		    //dump($result1);dump($usd_price3);die;
		    //PAI
		    $url2="https://api.huobi.pro/market/detail/merged?symbol=paiusdt";
		    $result2=$this->curl_get($url2);
		    $price4=$result2['tick']['close'];
		    //处理最新价
		    $sql = "
			UPDATE yang_price SET price1 = ".$price1.",price2=".$price2.",price3=".$price3.",price4=".$price4.",add_time = ".time();
		    $this->query($sql);
		}
		//每日奖励
		private function runFenhongMoneyDay($time){
		      $today = strtotime(date("Y-m-d"),time());//零点时间
                //判断时间是否应该结束
    		    //$sql = "select * from yang_project_log where status = 1 and add_time<".$today;
    		    //测试用
		       $sql = "select * from yang_project_log";
    		    $project = $this->select($sql);//谁可以拿
    		    //结算奖金
				if($project){
    				//结算静态
        		    foreach ($project as $k=>$v){
        		        $this->runStaticMoney($v,$time);
        		    }
    		    } 
    		    //$sql = "select * from yang_project_dongjie where status = 0 and add_time<".$today;
    		    //测试用
    		    // $sql = "select * from yang_project_dongjie where status = 0 and add_time<1544198400";
    		    // $dongjie_list = $this->select($sql);
    		    // //动态奖
    		    // if($dongjie_list){
        		    // foreach ($dongjie_list as $k=>$v){
        		        // $sql =  "select * from yang_project_log where id = ".$v['project_id'];
        		        // $type = $this->find($sql)['type'];
						// if($type){
							// $this->runDongjieMoney($v,$type,$time);
						// }
        		    // }
    		    // }
				 $sql = "select * from yang_project_dongjie where status = 0 and uid>8";
    		    $dongjie_list = $this->select($sql);
    		    //动态奖
    		    if($dongjie_list){
        		    foreach ($dongjie_list as $k=>$v){
        		        $sql =  "select * from yang_project_log where id = ".$v['project_id'];
        		        $type = $this->find($sql)['type'];
						if($type){
							$this->runDongjieMoney($v,$type,$time);
						}
        		    }
    		    }
    		    }
		//发静态收益
		private function runStaticMoney($user_project,$time){
		        $fanli_bili = $this->config['fanli'.$user_project['type']];
    		    $money = $user_project['money']*$fanli_bili;
    		    $sql= "select * from yang_price";
		        $pricelist = $this->find($sql);
		        //其他币价格
		        $usd_price = $pricelist['price'.$user_project['type']];
		        //能换多少个钱包币
    		    $shiji_money = ($money*$usd_price)/$pricelist['price1'];
    		    //钱包币
    		    if($user_project['type']==1){
    		        $shiji_money = $money;
    		    }
                //加钱
                $r = $this->setUserMoneyLogic($user_project['uid'],$shiji_money,1,'每日返利',1,11,$time);
               
		   // }
		}
		//动态
		private function runDongjieMoney($dongjie,$type1,$time){
			$type = $type1?$type1:1;
		    $uid 	= $dongjie['uid'];
		    $sql= "select * from yang_user_relation where uid = ".$uid;
		    $user_relation = $this->find($sql);
		    //直推几人拿几代
		    //直推一人
		    if($user_relation['p_num']==1){
		        // echo 1;
		        if($dongjie['daishu']>1){
		            return;
		        }
		        $fanli_bili = 0.5;
		    }
		    //直推两人 拿两代
		    if($user_relation['p_num']==2){
		        // echo 2;
		        if($dongjie['daishu']>2){
		            return;
		        }
		        if($dongjie['daishu']==1){
		            $fanli_bili = 1;
		        }
		        if($dongjie['daishu']==2){
		            $fanli_bili = 0.5;
		        }
		    }
		    //dump($user_relation);
		    /* dump($user_relation['p_num']);dump($k);
		     if($user_relation['p_num']==2&&$k==1){
		     dump(1);
		     }
		     if($user_relation['p_num']==2&&$k==2){
		     dump(2);
		     } */
		    if($user_relation['p_num']==3){
		        //echo 3;
		        if($dongjie['daishu']>3){
		            return;
		        }
		        if($dongjie['daishu']==1){
		            $fanli_bili = 1;
		        }
		        if($dongjie['daishu']==2){
		            $fanli_bili = 0.5;
		        }
		        if($dongjie['daishu']==3){
		            $fanli_bili = 0.2;
		        }
		         
		    }
		    if($user_relation['p_num']==4){
		        //echo 4;
		        if($dongjie['daishu']>4){
		            return;
		        }
		        if($dongjie['daishu']==1){
		            $fanli_bili = 1;
		        }
		        if($dongjie['daishu']==2){
		            $fanli_bili = 0.5;
		        }
		        if($dongjie['daishu']==3){
		            $fanli_bili = 0.2;
		        }
		        if($dongjie['daishu']==4){
		            $fanli_bili = 0.1;
		        }
		    }
		    if($user_relation['p_num']>=5){
		        //echo 5;
		        if($dongjie['daishu']==1){
		            $fanli_bili = 1;
		        }
		        if($dongjie['daishu']==2){
		            $fanli_bili = 0.5;
		        }
		        if($dongjie['daishu']==3){
		            $fanli_bili = 0.2;
		        }
		        if($dongjie['daishu']==4){
		            $fanli_bili = 0.1;
		        }
		        if($dongjie['daishu']>=5){
		            $fanli_bili = 0.1;
		        }
		    }
		    if($user_relation['p_num']>=10){
		        if($dongjie['daishu']==1){
		            $fanli_bili = 1;
		        }
		        if($dongjie['daishu']==2){
		            $fanli_bili = 0.5;
		        }
		        if($dongjie['daishu']==3){
		            $fanli_bili = 0.2;
		        }
		        if($dongjie['daishu']==4){
		            $fanli_bili = 0.1;
		        }
		        if($dongjie['daishu']>=5){
		            $fanli_bili = 0.1;
		        }
		        if($dongjie['daishu']>=10){
		            $fanli_bili = 0.05;
		        }
		    }
		    //dump($fanli_bili);
		    $fanli_bili_config = $this->config['fanli'.$type];
		    $money  = $dongjie['money']*$fanli_bili*$fanli_bili_config;
		    //实际应该给多少钱包币
	        $sql= "select * from yang_price";
	        $pricelist = $this->find($sql);
	        //其他币价格
	        $usd_price = $pricelist['price'.$type];
	        //能换多少个钱包币
		    $shiji_money = ($money*$usd_price)/$pricelist['price1'];
		    //钱包币
		    if($type==1){
		        $shiji_money = $money;
		    }
		    //团队奖
		    $this->runTuanduiMoney($uid,$shiji_money,$time,$dongjie,1);
		    //社区奖励 业绩达到某个值得到奖励
		    $sql = "select * from yang_user_relation where uid = ".$uid;
		    $user_relation = $this->find($sql);
		    if($user_relation['yeji']>10000&&$user_relation['yeji']<30000){
		        $bili = 0.01;
		    }elseif($user_relation['yeji']>=30000&&$user_relation['yeji']<50000){
		        $bili = 0.03;
		    }elseif($user_relation['yeji']>=50000&&$user_relation['yeji']<100000){
		        $bili = 0.05;
		    }elseif($user_relation['yeji']>=100000&&$user_relation['yeji']<200000){
		        $bili = 0.07;
		    }elseif($user_relation['yeji']>=200000&&$user_relation['yeji']<1000000){
		        $bili = 0.09;
		    }elseif($user_relation['yeji']>=1000000){
		        $bili = 0.15;
		    }else{
		        $bili = '';
		    }
		    if($bili){
		        $shequ_money  = $dongjie['money']*$fanli_bili_config;
		        $shouyi_money = $shequ_money*$bili;
		        //实际应该给多少钱包币
		        $shiji_money = ($shouyi_money*$usd_price)/$pricelist['price1'];
		        if($type==1){
		            $shiji_money = $shouyi_money;
		        }
		        $this->runShouyiMoney($uid,$shiji_money,$time,$dongjie,1);
		    }
		}
		//满足条件发放团队奖
		private function runTuanduiMoney($uid,$money,$time,$dongjie,$type){
		    $this->setUserMoneyLogic($uid, $money, 1, '来自'.$dongjie['daishu'].'级会员'.$dongjie['laizi_username'].'的分享奖励',$type,22,$time);
		
		}
		//返还收益的比例
		private function runShouyiMoney($uid,$money,$time,$dongjie,$type){
		    $this->setUserMoneyLogic($uid, $money, 1, '来自'.$dongjie['daishu'].'级会员'.$dongjie['laizi_username'].'的社区奖励',$type,33,$time);
		
		}
		/**
		 * 修改用户资金并增加财务日志
		 * @param unknown $uid
		 * @param unknown $money		变动金额
		 * @param unknown $type		收支 1收入  2支出
		 * @param unknown $content		财务日志内容
		 * @param unknown $money_type	币种类型 1人民币
		 * @param unknown $project_id	项目id
		 */	
		private function setUserMoneyLogic($uid,$money,$type,$content,$money_type,$finance_type,$time){
		    if($money_type == 1){
		        $fuhao = " + ";
		    }else{
		        $fuhao = " - ";
		    }
		    //处理金额变动
		    $sql ="
			UPDATE yang_member_money
			SET money1 = money1 +".$money."  
			WHERE member_id =".$uid;
		    $this->query($sql);
		    //处理财务日志变动
		    $sql = "
			INSERT INTO yang_finance_".$this->cuttable($uid)."
			(member_id,type,money_type,money,content,add_time,currency_id)
			VALUES ('".$uid."','".$type."','".$money_type."','".$money."','".$content."','".$time."','".$finance_type."')
			";
		    $this->query($sql);
		    //加总收益
		    $sql = "
			UPDATE yang_user_relation
			SET shouyi = shouyi"."+".$money."  
			WHERE uid =".$uid;
		    //dump($sql);
		    $this->query($sql);
		}
		public function getConfigByKey($key){
		    $sql = "select key,value from yang_config where key = ".$key;
		    $config = $this->find($sql)['value'];
		    return $config;
		}
		//分表原则 3.10 zy
		private function cuttable($number){
		    return $number%10;
		}
		//使用curl的GET请求数据  返回数组
		public function curl_get($http){
		    //初始化
		    $ch = curl_init();
		    //设置选项，包括URL
		    curl_setopt($ch, CURLOPT_URL, $http);
		    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		    curl_setopt($ch, CURLOPT_HEADER, 0);
		    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//这个是重点。https不好使加这个
		    //执行并获取HTML文档内容
		    $output = curl_exec($ch);
		    //释放curl句柄
		    curl_close($ch);
		    //打印获得的数据
		    $result = json_decode($output,true);
		    return $result;
		}
	}
	$auto=new auto();
	$auto->run();
?>